<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: admin.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/*
 * Based on code from http://davidwalsh.name/backup-mysql-database-php, but
 * heavily modified to work with the DB abstraction layer we use...
 */

// backup the db based on prefix for table names
function backup_tables($db, $dbprefix, $path) {
  
    //get all of the tables
    $tables = array();
    $result = $db->sql_query('SHOW TABLES');
    while($row = $db->sql_fetchrow($result)) {
        if (eregi("^$dbprefix", $row[0])) {
            $tables[] = $row[0];
        }
    }
  
    $return = '';
    //cycle through
    foreach($tables as $table) {
        $result     = $db->sql_query('SELECT * FROM '.$table);
        $num_fields = $db->sql_numfields($result);
        
        $return .= 'DROP TABLE '.$table.';';
        $res2   = $db->sql_query('SHOW CREATE TABLE '.$table);
        $row2   = $db->sql_fetchrow($res2);
        $return .= "\n\n".$row2[1].";\n\n";
        
        for ($i = 0; $i < $num_fields; $i++) {
            while($row = $db->sql_fetchrow($result)) {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                    if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }
    
    //save file
    $fname = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
    $handle = fopen($path.'/'.$fname,'w+');
    fwrite($handle,$return);
    fclose($handle);

    return $fname;
}


$auth	= 'false';

//
// Support working in phpsuexec mode
//
if (isset($_SERVER["HTTP_AUTHORIZATION"]) && !empty($_SERVER["HTTP_AUTHORIZATION"])) {
	list ($type, $cred) = split (" ", $_SERVER['HTTP_AUTHORIZATION']);
	
	if ($type == 'Basic') {
		list ($user, $pass) = explode (":", base64_decode($cred));
	}
} else if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
	$user	= $_SERVER['PHP_AUTH_USER'];
	$pass	= $_SERVER['PHP_AUTH_PW'];
}

$res	= checkAuth($user, $pass, -1, 1);
if (is_array($res)) {
	$auth	= 'true';
} else {
	$auth	= 'false';
}

if ($auth == 'false') {
	Header('WWW-Authenticate: Basic realm="StormChat Admin - ' . $config['FORUM_NAME'] . '"');
	Header('HTTP/1.0 401 Unauthorized');
	print('Authorization Required.');
	exit;
}

$admin_header	= 1;
if (!isset($skip_header)) {
    showHeader();
}

